<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script type="text/javascript">
			var frequencySort = function(nums) {
				var map = {}; //记录次数
				var res;
				for (let i of nums) {
					if (map[i]) {
						map[i] = map[i] + 1
					} else {
						map[i] = 1
					}
				}
				res = nums.sort(function(a, b) {
					if (map[a] == map[b]) { //出现频率一样按数值排序
						return b - a
					} else { //出现频率不同则按次数大小排序
						return map[a] - map[b]
					}
				})
				return res;
			};
			console.log(frequencySort([1, 1, 2, 2, 2, 3]));
		</script>
	</body>
</html>
